package com.zero.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.logistics.pojo.BaseData;
import com.zero.logistics.pojo.Customer;
import com.zero.logistics.pojo.CustomerExample;
import com.zero.logistics.pojo.MessageObject;
import com.zero.logistics.pojo.Order;
import com.zero.logistics.pojo.OrderDetail;
import com.zero.logistics.pojo.OrderExample;
import com.zero.logistics.pojo.OrderExample.Criteria;
import com.zero.logistics.pojo.User;
import com.zero.logistics.pojo.UserExample;
import com.zero.logistics.service.BaseDataService;
import com.zero.logistics.service.CustomerService;
import com.zero.logistics.service.OrderService;
import com.zero.logistics.service.UserService;
import com.zero.logistics.utils.Constant;

/*
 *	@date:2020年3月31日 上午5:00:13
 *	@author:Zero
 *	@version:Future
 *	@description: 
 *
 */
@Controller
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private UserService userService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private BaseDataService baseDataService;

	@RequiresPermissions("order:orderPage")
	@RequestMapping("/orderPage")
	public String orderPage() {
		return "orderPage";
	}
	
	/**
	 * @function 分页显示订单
	 * @param pageNum
	 * @param pageSize
	 * @param keywords
	 * @return
	 */
	@RequiresPermissions("order:list")
	@RequestMapping("list")
	@ResponseBody  //返回json类型的数据
	public PageInfo<Order> orderList(@RequestParam(defaultValue = "1")int pageNum,
			@RequestParam(defaultValue = "10")int pageSize,
			String keywords){
		
		PageHelper.startPage(pageNum, pageSize);
		OrderExample example = new OrderExample();
		
		// 定义搜索的逻辑（使用管理）
		if (StringUtils.isNotBlank(keywords)) {  //当前台传过来的关键字不等于空的时候
			//根据订单名查询
			Criteria criteria1 = example.createCriteria();
			criteria1.andShippingNameLike("%" + keywords + "%");
		}
		
		List<Order> orders = orderService.selectByExample(example);
		PageInfo<Order> pageInfo = new PageInfo<>(orders);
		
		return pageInfo;
	}
	
	@RequiresPermissions("order:detail")
	@RequestMapping("/detail")
	@ResponseBody
	public List<OrderDetail> OrderDetail(Long orderId){
		List<OrderDetail> orderDetails = orderService.selectDetailsByOrderId(orderId);
		return orderDetails;
	}
	
	/**
	 * @function 删除订单
	 * @param orderId
	 * @return
	 */
	@RequiresPermissions("order:delete")
	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject orderDelete(Long orderId) {
		//1.根据订单id删除订单
		int row = orderService.deleteByPrimaryKey(orderId);
		MessageObject mo = MessageObject.createMo(0, "删除数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "删除数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 批量删除(以String类型接收前端传过来的参数)
	 * @param map
	 * @return
	 */
	@RequiresPermissions("order:batchdel")
	@RequestMapping("/batchdel")
	@ResponseBody
	public MessageObject orderBatchdel(String orderIds) {
		System.out.println(orderIds);
		String[] orderIdsArr = orderIds.split(",");
		
		//1.根据订单id的数组批量删除订单
		int rows = orderService.deleteByIds(orderIdsArr);
		MessageObject mo = MessageObject.createMo(0, "批量删除数据失败，请联系技术人员");
		if (rows != 0) {
			mo = MessageObject.createMo(1, "批量删除数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 提供编辑页面(在新增订单和更新订单的时候都需要执行此方法)
	 * @param m
	 * @return
	 */
	@RequestMapping("/edit")
	public String orderEdit(Model m, Long orderId) {
		/*
		 * 1.查询出需要回显给前端的数据
		 * 	1.1 业务员-->userService
		 * 	1.2 客户-->customerService
		 * 	1.3 到达区域-->baseDataService
		 *  1.4 付款方式-->baseDataService
		 *  1.5 货运方式-->baseDataService
		 *  1.6 取件方式-->baseDataService
		 *  1.7 单位-->baseDataService
		 */
		 // 1.1 业务员-->userService
		UserExample userExample = new UserExample();
		com.zero.logistics.pojo.UserExample.Criteria criteria1 = userExample.createCriteria();
		criteria1.andRolenameLike(Constant.ROLE_SALESMAN);
		List<User> users = userService.selectByExample(userExample);
		m.addAttribute("users", users);
		// 1.2 客户-->customerService
		CustomerExample customerExample = new CustomerExample();
		List<Customer> customers = customerService.selectByExample(customerExample);
		m.addAttribute("customers", customers);
		// 1.3 到达区域（区间管理）-->baseDataService
		List<BaseData> regions = baseDataService.selectBaseDataByParentName(Constant.BASE_DATA_REGION);
		m.addAttribute("regions", regions);
		// 1.4 付款方式-->baseDataService
		List<BaseData> payments = baseDataService.selectBaseDataByParentName(Constant.BASE_DATA_PAYMENT);
		m.addAttribute("payments", payments);
		// 1.5 货运方式-->baseDataService
		List<BaseData> freights = baseDataService.selectBaseDataByParentName(Constant.BASE_DATA_FREIGHT);
		m.addAttribute("freights", freights);
		// 1.6 取件方式-->baseDataService
		List<BaseData> fetchTypes = baseDataService.selectBaseDataByParentName(Constant.BASE_DATA_FETCHTYPE);
		m.addAttribute("fetchTypes", fetchTypes);
		// 1.7 单位-->baseDataService
		List<BaseData> units = baseDataService.selectBaseDataByParentName(Constant.BASE_DATA_UNIT);
		m.addAttribute("units", units);
		
		//2.当前端传过来的orderId不为空的时候，就通过orderId查询订单信息并共享数据，用于更新的情况
		if (orderId != null) {
			Order order = orderService.selectByPrimaryKey(orderId);
			System.out.println("需要更新的订单信息：" + order.getOrderId());
			m.addAttribute("order", order);
		}

		return "orderEdit";
	}
	
	
	/**
	 * @function 新增订单
	 * @param order
	 * @return
	 */
	@RequiresPermissions("order:insert")
	@RequestMapping("/insert")
	@ResponseBody
	public MessageObject orderInsert(@RequestBody Order order) {  //@RequestBody可以将接收的数据转成json对象
		System.out.println("新增的订单：" + order);
		System.out.println("新增的订单明细" + order.getOrderDetails());
		
		// 1.插入数据并返回结果
		int row = orderService.insert(order);
		MessageObject mo = MessageObject.createMo(0, "添加数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "添加数据成功");
		}
		System.out.println(mo.getMsg());
		return mo;
	}
	
	/**
	 * @function 更新订单信息
	 * @param order
	 * @return
	 */
	@RequiresPermissions("order:update")
	@RequestMapping("update")
	@ResponseBody
	public MessageObject orderUpdate(Order order) {
		// 1.更新数据并返回结果(这里要用动态sql语句)
		int row = orderService.updateByPrimaryKeySelective(order);
		MessageObject mo = MessageObject.createMo(0, "更新数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "更新数据成功");
		}
		return mo;
	}

}
